﻿using Business.BorrowManagement;
using Business.BorrowManagement.Dto;
using Business.Models;
using Business.RGoodManagement.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using XCZ;
using Yitter.IdGenerator;

namespace Business.RGoodManagement
{
    public class RGoodAppService : BusinessWorkFlowAppService, IRGoodAppService
    {
        private const string FormName = "BorrowM";
        private IRepository<RGoods, Guid> _repository;
        private IRepository<RDetail, Guid> _repository1;
        private IRepository<BorrowM, Guid> _repository2;
        private IRepository<BorrowPro, Guid> _repository3;
        public RGoodAppService(
            IRepository<RGoods, Guid> repository, IRepository<RDetail, Guid> repository1, IRepository<BorrowM, Guid> repository2, IRepository<BorrowPro, Guid> repository3
            )
        {
            _repository = repository;
            _repository1 = repository1;
            _repository2 = repository2;
            _repository3 = repository3;
        }
        public async Task<RGoodDto> Get(Guid id)
        {
            var query = (from b in await _repository.GetQueryableAsync()
                         where id == b.Id
                         // on b.TenantId equals d.BorrowId
                         select new RGoodDto
                         {
                             Id = b.Id,
                             ConUnit = b.ConUnit,
                             RNum = b.RNum,
                             RTitle = b.RTitle,
                             RPeo = b.RPeo,
                             RTime = b.RTime,
                             RHand = b.RHand,

                             RConP = b.RConP,
                             RDesc = b.RDesc,

                             createOrUpdateRGoodDto = (from d in _repository1.GetQueryableAsync().Result
                                              where b.Id == d.RGoodId
                                              select new CreateOrUpdateRGoodDto
                                              {
                                                  RPName = d.RPName,
                                                  RPNum = d.RPNum,
                                                  RSNum = d.RSNum,
                                                  RUnit = d.RUnit,
                                                  RNum = d.RNum,
                                                  RTime = d.RTime,
                                                  RBase = d.RBase,
                                                  RPH = d.RPH,
                                                  RPack = d.RPack,
                                                  RDesc = d.RDesc,
                                              }
                             ).ToList()
                         }
                   ).ToList();



            //var data = await _repository.GetAsync(id);
            //var dto = ObjectMapper.Map<BorrowM, BorrowDto>(data);
            return query.ToList()[0];
        }

        public async Task<PagedResultDto<RGoodDto>> GetAll(GetRGoodInputDto input)
        {
            //var query = (await _repository.GetQueryableAsync()).WhereIf(!string.IsNullOrWhiteSpace(input.Filter), a => a.BTitle.Contains(input.Filter));

            var query = (from b in await _repository.GetQueryableAsync()
                        
                         // on b.TenantId equals d.BorrowId
                         select new RGoodDto
                         {
                             Id = b.Id,
                             ConUnit = b.ConUnit,
                             RNum = b.RNum,
                             RTitle = b.RTitle,
                             RPeo = b.RPeo,
                             RTime = b.RTime,
                             RHand = b.RHand,

                             RConP = b.RConP,
                             RDesc = b.RDesc,

                             createOrUpdateRGoodDto = (from d in _repository1.GetQueryableAsync().Result
                                                       where b.Id == d.RGoodId
                                                       select new CreateOrUpdateRGoodDto
                                                       {
                                                           RPName = d.RPName,
                                                           RPNum = d.RPNum,
                                                           RSNum = d.RSNum,
                                                           RUnit = d.RUnit,
                                                           RNum = d.RNum,
                                                           RTime = d.RTime,
                                                           RBase = d.RBase,
                                                           RPH = d.RPH,
                                                           RPack = d.RPack,
                                                           RDesc = d.RDesc,
                                                       }
                             ).ToList()
                         }
                   ).ToList();



            var totalCount = query.Count();
            var items = query.Skip(input.SkipCount - 1).Take(input.MaxResultCount).ToList();

            //var dtos = ObjectMapper.Map<List<BorrowM>, List<BorrowDto>>(items);
            return new PagedResultDto<RGoodDto>(totalCount, query);
        }
     

        public async Task<RGoodDto> CreateOrUpdate(RGoodDto input)
        {
            RGoods result = null;
            if (!input.Id.HasValue)
            {
                input.Id = GuidGenerator.Create();
                //雪花Id
                input.RNum = YitIdHelper.NextId().ToString();
                input.RTime=DateTime.Now;   
                RGoods borrow = new RGoods()
                {
                    ConUnit = input.ConUnit,
                    RNum = input.RNum,
                    RTitle = input.RTitle,
                    RPeo = input.RPeo,
                    RTime = input.RTime,
                    RHand = input.RHand,
                    BorrowId=input.BorrowId,
                    RConP = input.RConP,
                    RDesc = input.RDesc,

                };
                foreach (var item in input.createOrUpdateRGoodDto)
                {
                    RDetail detail = new RDetail()
                    {
                       
                        RGoodId = input.Id,
                        RPName = item.RPName,
                        RPNum = item.RPNum,
                        RSNum = item.RSNum,
                        RUnit = item.RUnit,
                        RNum = item.RNum,
                        RTime = item.RTime,
                        RBase = item.RBase,
                        RPH = item.RPH,
                        RPack = item.RPack,
                        RDesc = item.RDesc,
                    };
                    await _repository1.InsertAsync(detail);

                };
                result = await _repository.InsertAsync(ObjectMapper.Map<RGoodDto, RGoods>(input));

                //创建工作流
                // await WorkFlow.CreateWorkFlow(FormName, result);
            }
            else
            {
                var data = await _repository.GetAsync(input.Id.Value);

                //BorrowM borrow = new BorrowM()
                //{
                //    BTitle = input.BTitle,
                //    BNum = input.BNum,
                //    BPeo = input.BPeo,
                //    BTme = input.BTme,
                //    BHand = input.BHand,
                //    BCon = input.BCon

                //};
                //borrow=await _repository.UpdateAsync(borrow);
                //foreach (var item in input.borrowDetails)
                //{
                //    BorrowPro detail = new BorrowPro()
                //    {
                //        BorrowId =(Guid) input.Id,
                //        PName = item.PName,
                //        PNum = item.PNum,
                //        SNum = item.SNum,
                //        BUnit = item.BUnit,
                //        BorNum = item.BorNum,
                //        GoBit = item.GoBit,
                //        BitInfo = item.BitInfo,
                //        GiveTime = item.GiveTime,
                //        PageStyle = item.PageStyle,
                //        Desc = item.Desc
                //    };
                //    await _repository1.UpdateAsync(detail);

                //};


                result = await _repository.UpdateAsync(ObjectMapper.Map(input, data));
                //检查流程状态
                //await WorkFlow.CheckFormStatus(input.Id.Value, result);
            }
            return ObjectMapper.Map<RGoods, RGoodDto>(result);
        }

        public async Task Delete(List<Guid> ids)
        {
            foreach (var item in ids)
            {
                await _repository.DeleteAsync(item);
            }

        }
    }
}
